package com.cai.commons.utils;

import com.alibaba.fastjson.JSON;
import com.cai.commons.utils.pri.HtmlDefult;

import java.io.File;
import java.io.IOException;

public class Utils {
    private static IoUtils ioUtils=new IoUtils();
    private static HtmlDefult htmlDefult=new HtmlDefult();
    private static final File HTMLDIR= new File("D:\\htmltemplate\\");
    private static final File HTMLUP=new File(HTMLDIR+"template_up.html") ;
    private static final File HTMLNEXT=new File(HTMLDIR+"template_next.html") ;

    /**
     * @Author CWX
     * @Date 2017/7/23 22:48
     * 判断文件是否存在
     * 只要有选中文件中一个不存在所有文件全部改为默认
     *返回false
     */
    private static void file() throws IOException {
       if (!HTMLDIR.exists()){//目录不存在
           HTMLDIR.mkdirs(); }
           if (!HTMLUP.exists()||!HTMLNEXT.exists()){
            HTMLUP.createNewFile();
            HTMLNEXT.createNewFile();
           }
    }

    /**
     * @Author CWX
     * @Date 2017/7/26 16:47
     * html制作
     */
    public static void html(File pathDir,String fileName,String title,String text) {
        try {
        file();
        String up=ioUtils.reader(HTMLUP);
        String result="<title>"+title+"</title><link href=\"/static/bootstrap/css/bootstrap.min.css\" rel=\"stylesheet\"/>"+"<body>"+text;
        String next=ioUtils.reader(HTMLNEXT);
        ioUtils.writer(pathDir,fileName,up+result+next);
        }catch (IOException e){
            System.out.println("html5制作失败");
        }
    }
    public static boolean js(Object saveFile,String filePathDir,String fileName)  {
        String resource=fileName;
        String json= JSON.toJSONString(saveFile);
        try {
            ioUtils.writer(new File(filePathDir),resource,json );
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("js制作失败");
            return false;
        }
        return true;
    }
}
